package com.fuint.utils;

import cn.hutool.core.map.MapUtil;
import org.apache.commons.lang.StringUtils;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

public class PropertiesUtil {

    // public static final ResourceBundle messageResource = ResourceBundle.getBundle("international.message", Locale.getDefault());

    private static final Map<Integer, String> messageMapping = MapUtil.ofEntries(
            MapUtil.entry(200, "\u8BF7\u6C42\u6210\u529F"),
            MapUtil.entry(201, "\u8BF7\u6C42\u5931\u8D25"),
            MapUtil.entry(202, "\u53C2\u6570\u6709\u8BEF"),
            MapUtil.entry(402, "\u7528\u6237\u4E0D\u5B58\u5728"),
            MapUtil.entry(403, "\u7528\u6237\u540D\u6216\u5BC6\u7801\u6709\u8BEF"),
            MapUtil.entry(500, "\u7CFB\u7EDF\u51FA\u9519\u5566"),
            MapUtil.entry(1001, "\u7528\u6237\u672A\u767B\u5F55"),
            MapUtil.entry(1002, "\u9A8C\u8BC1\u7801\u9519\u8BEF\u6216\u8D85\u65F6"),
            MapUtil.entry(1003, "\u5361\u5238\u6838\u9500\u5931\u8D25"),
            MapUtil.entry(1004, "\u60A8\u6CA1\u6709\u8BBF\u95EE\u6743\u9650"),
            MapUtil.entry(1005, "\u5361\u5238\u72B6\u6001\u5F02\u5E38"),
            MapUtil.entry(1006, "\u5361\u5238\u9886\u53D6\u5931\u8D25"),
            MapUtil.entry(2000, "\u7CFB\u7EDF\u51FA\u9519\u5566"),
            MapUtil.entry(2001, "\u4F1A\u5458\u6CE8\u518C\u5931\u8D25"),
            MapUtil.entry(3000, "\u5FAE\u4FE1\u652F\u4ED8\u914D\u7F6E\u5F02\u5E38"),
            MapUtil.entry(4000, "\u60A8\u6CA1\u6709\u6536\u94F6\u6743\u9650"),
            MapUtil.entry(5001, "\u4F1A\u5458\u4F59\u989D\u4E0D\u8DB3")
    );

    /**
     * 获取请求返回Code对应的Message
     * @param code
     * @param params
     * @return
     */
    public static String getResponseErrorMessageByCode(int code, String... params) {
        // String pStr = messageResource.getString("response.error." + code);
        String pStr = messageMapping.get(code);
        if (StringUtils.isEmpty(pStr) || pStr == null) {
            return "";
        }
        if (params == null || params.length == 0) {
            return pStr;
        }
        MessageFormat format = new MessageFormat(pStr, Locale.getDefault());
        return format.format(params);
    }

    /**
     * 根据Key值获取Value
     * @param key
     * @param params
     * @return
     */
    public static String getValueByKey(String key, String... params) {
        String pStr = "";
        //String pStr = messageResource.getString(key);
        //if (messageResource == null) {
        //    return "";
        //}
        if (StringUtils.isEmpty(pStr)) {
            return "";
        }
        if (params == null || params.length == 0) {
            return pStr;
        }
        MessageFormat format = new MessageFormat(pStr, Locale.getDefault());
        return format.format(params);
    }
}
